//
//  PartBuyBottomView.swift
//  TianTianBiTing
//
//  Created by Gox mac 2 on 2019/1/9.
//  Copyright © 2019 Gox_WallyDiao. All rights reserved.
//

import UIKit

protocol PartBuyBottomViewDelegate:NSObjectProtocol {
    func buyBtnActionDele()
    func centreBtnActionDele(tag:Int)
    
}

class PartBuyBottomView: UIView {
    
    var delegate:PartBuyBottomViewDelegate?

    lazy var topLabel:UILabel = {
        let label = UILabel.init(frame: FRAME_CGFloat(0, 0, SCREEN_WIDTH, 44))
        label.font = FONT(17)
        label.textColor = RGB_Same(53)
        label.backgroundColor = RGB_Same(243)
        label.textAlignment = .center
        return label
    }()
    
    lazy var leftBtn:UIButton = {
        let button = UIButton(type: UIButton.ButtonType.custom)
        button.frame = FRAME_CGFloat(SCREEN_WIDTH / 2 - 160, 54, 100, 39)
        button.setTitle("本集购买", for: .normal)
        button.addTarget(self, action: #selector(centreBtnAction(btn:)), for: .touchUpInside)
        button.tag = 31
        button.layer.cornerRadius = 3
        button.layer.borderWidth = 1
        return button
    }()
    lazy var centreBtn:UIButton = {
        let button = UIButton(type: UIButton.ButtonType.custom)
        button.frame = FRAME_CGFloat(SCREEN_WIDTH / 2 - 50, 54, 100, 39)
        button.setTitle("10集购买", for: .normal)
        button.addTarget(self, action: #selector(centreBtnAction(btn:)), for: .touchUpInside)
        button.tag = 32
        button.layer.cornerRadius = 3
        button.layer.borderWidth = 1
        return button
    }()
    lazy var rightBtn:UIButton = {
        let button = UIButton(type: UIButton.ButtonType.custom)
        button.frame = FRAME_CGFloat(SCREEN_WIDTH / 2 + 60, 54, 100, 39)
        button.setTitle("自定义", for: .normal)
        button.addTarget(self, action: #selector(centreBtnAction(btn:)), for: .touchUpInside)
        button.tag = 33
        button.layer.cornerRadius = 3
        button.layer.borderWidth = 1
        return button
    }()
    lazy var autoBtn:UIButton = {
        let button = UIButton(type: UIButton.ButtonType.custom)
        button.frame = FRAME_CGFloat(SCREEN_WIDTH - 25, 118, 15, 15)
        button.addTarget(self, action: #selector(autoBtnAction), for: .touchUpInside)
        button.setImage(IMAGE(name: "icon_noChoose"), for: .normal)
        button.isHidden = true
        return button
    }()
    lazy var buyBtn:UIButton = {
        let button = UIButton(type: UIButton.ButtonType.custom)
        button.frame = FRAME_CGFloat(SCREEN_WIDTH / 2, 196.5, SCREEN_WIDTH / 2, 54)
        button.setTitle("购买", for: .normal)
        button.titleLabel?.font = FONT(18)
        button.setTitleColor(WhiteBackColor, for: .normal)
        button.addTarget(self, action: #selector(buyBtnAction), for: .touchUpInside)
        button.backgroundColor = TabbarTintColor
        return button
    }()
    
    lazy var moneyLabel:UILabel = {
        let label = UILabel.init(frame: FRAME_CGFloat(0, 196.5, SCREEN_WIDTH / 2, 54))
        label.font = FONT(18)
        label.textColor = RGB_Same(53)
        label.backgroundColor = WhiteBackColor
        label.textAlignment = .center
        return label
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = WhiteBackColor
        self.addSubview(topLabel)
        self.addSubview(leftBtn)
        self.addSubview(centreBtn)
        self.addSubview(rightBtn)
        self.addSubview(autoBtn)
        self.addSubview(buyBtn)
        self.addSubview(moneyLabel)
        setNoChoose(btn: leftBtn)
        setNoChoose(btn: centreBtn)
        setNoChoose(btn: rightBtn)
        setUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}


extension PartBuyBottomView {
    func setUI() {
        let label1 = UILabel.init(frame: FRAME_CGFloat(16, 115.5, 110, 20))
        label1.font = FONT(14)
        label1.textColor = RGB_Same(53)
        label1.text = "播放时自动购买"
        label1.isHidden = true
        self.addSubview(label1)
        
        let lineView = UIView.init(frame: FRAME_CGFloat(0, 146.5, SCREEN_WIDTH, 1))
        lineView.backgroundColor = RGB_Same(240, 1)
        self.addSubview(lineView)
        
        let label2 = UILabel.init(frame: FRAME_CGFloat(13, 157.5, 230, 27))
        label2.font = FONT(11)
        label2.textColor = RGB_Same(136)
        label2.text = "1.数字音频节目购买成功后不可退款\n2.快捷选集购买已自动跳过你无需购买的节目"
        label2.numberOfLines = 2
        self.addSubview(label2)
        
        let button = UIButton(type: UIButton.ButtonType.custom)
        button.frame = FRAME_CGFloat(SCREEN_WIDTH - 25, 10, 15, 15)
        button.addTarget(self, action: #selector(hiddenAction), for: .touchUpInside)
        button.setImage(IMAGE(name: "close"), for: .normal)
        self.addSubview(button)
        
    }
    
    @objc func centreBtnAction(btn:UIButton) {
        delegate?.centreBtnActionDele(tag: btn.tag)
        switch btn.tag {
        case 31:
            setChoose(btn: leftBtn)
            setNoChoose(btn: centreBtn)
        case 32:
            setNoChoose(btn: leftBtn)
            setChoose(btn: centreBtn)
        default:
            return
        }
        
    }
    
    @objc func hiddenAction() {
        self.isHidden = true
    }
    
    @objc func autoBtnAction() {
        autoBtn.isSelected = !autoBtn.isSelected
        if autoBtn.isSelected {
            autoBtn.setImage(IMAGE(name: "icon_choose"), for: .normal)
           
        } else {
            autoBtn.setImage(IMAGE(name: "icon_noChoose"), for: .normal)
            
        }
        
    }
    
    @objc func buyBtnAction() {
        delegate?.buyBtnActionDele()
    }
    
    func setChoose(btn:UIButton) {
        btn.layer.borderColor = RGB(230, 67, 64).cgColor
        btn.setTitleColor(RGB(230, 67, 64), for: .normal)
    }
    
    func setNoChoose(btn:UIButton) {
        btn.layer.borderColor = RGB_Same(240).cgColor
        btn.setTitleColor(RGB_Same(53), for: .normal)
    }
    
    func setFirstChoose() {
        setChoose(btn: leftBtn)
        setNoChoose(btn: centreBtn)
        setNoChoose(btn: rightBtn)
    }
}
